home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-07-29 | 10.7 KB | 373 lines | [TEXT/PJMM] |
- program PCalendar;
-
- {23 july 1994, I found Ken Longs submission of NewCalendar in alt.sources.mac. The program}
- {was hard-coded for displaying the 1994 calendar, and used custom routines for calculating the}
- {dates. I was in playing mood, I guess, so I re-used parts of NewCalendar to make a similar program,}
- {producing the same output, but asking for what year to make a calendar for, and places the result}
- {in the clipboard (so you can paste it in a program that can print it). It now uses toolbox calls, which}
- {means that it should work until the year 2040. I have tested it on 1993 to 1996.}
-
-
- {The following is from NewCalendar:}
-
- {• --------------------------------------------------------------- •//}
- {• Feb 11, 1994 •//}
- {• Kenneth A. Long •//}
- {• Added rectangles around months drawn in test. •//}
- {• Added drawing of last 3 months which original test did not do. •//}
- {• Made test draw current year. •//}
- {• Added comments. •//}
- {• Removed calendar library, which was built with Calendar.c and .h}
- {• so the source could be traced.}
- {• --------------------------------------------------------------- •//}
-
- {• --------------------------------------------------------------- •//}
- {• Calendar test main.c •//}
- {• Simple and quick test of caldendar routines •//}
- {• --------------------------------------------------------------- •//}
-
-
- uses
- Calendar;
-
- {Resource IDs}
- const
- kWeekdayStrings = 128;
- kMonthStrings = 129;
- kCalendarWindowId = 128;
- kYearDlogID = 128;
- kDaysStringId = 128;
-
- {Constants for size of the boxes}
- const
- kRectHeight = 104;
- kRectWidth = 124;
- kHSpacing = 2;
- kVSpacing = 10;
- kFirstHOffset = 3;
- kFirstVOffset = 23; {7}
-
- {Globals}
- var
- gMonthDays: array[1..12] of integer;
-
- {Strings read from resources:}
- gpDaysOfWeek: array[1..7] of Str255;
- gpMonthsOfYear: array[1..12] of Str255;
- gDaysString: Str255;
-
- {• --------------------------------------------------------------- •//}
- {• CenterWindow() •//}
- {• --------------------------------------------------------------- •//}
-
- procedure CenterWindow (theDialog: WindowPtr);
- var
- centerPos: Point;
- dialogRect: Rect;
- desktopExtent: Rect;
- begin
- desktopExtent := GetGrayRgn^^.rgnBBox;
-
- {• get dialog's rectangle •//}
- dialogRect := theDialog^.portRect;
-
- {• calculate screen center position •//}
- with desktopExtent do
- SetPt(centerPos, (right + left) div 2, (bottom - top) div 3 + top);
-
- {• center dialog's rectangle •//}
- MoveWindow(theDialog, centerPos.h - (dialogRect.right - dialogRect.left) div 2, centerPos.v - (dialogRect.bottom - dialogRect.top) div 3, FALSE);
-
- end; {• CenterDialog() •//}
-
- {• --------------------------------------------------------------- •//}
- {• SetDefaultText() •//}
- {• --------------------------------------------------------------- •//}
-
- procedure SetDefaultText (fn: integer; fc: Style; sz: integer);
- begin
- TextFont(fn);
- TextFace(fc);
- TextSize(sz);
- end;
-
- {• --------------------------------------------------------------- •//}
- {• DrawMonth() •//}
- {• DrawMonth() will draw theMonth or theYear at location start in •//}
- {• in thePort. •//}
- {• --------------------------------------------------------------- •//}
-
- procedure DrawMonth (thePort: GrafPtr; start: Point; theMonth: Integer; theYear: Integer);
- var
- oldPort: GrafPtr;
- fInfo: FontInfo;
- rowSize, currRow, ddayOfMonth, numDays, dayIndex, horizCenter, mWidth: Integer;
- pos: Point;
- tmpstr: Str255;
- tmp: integer;
-
- procedure DrawDay (dn, dw: integer);
- begin
- MoveTo(pos.h + (dw - 1) * StringWidth(' '), pos.v + rowSize * currRow);
- NumToString(dn, tmpstr);
- if ord(tmpstr[0]) = 1 then
- DrawString(' ');
- DrawString(tmpstr);
- end; {DrawDay}
-
- begin
- currRow := 1;
-
- {• DrawDay() - 'Neat' (in the clean sense) macro to draw day }
- {• number under the correct day of week.}
-
- if ((theMonth <= 0) or (theMonth > 12)) then
- begin
- exit(DrawMonth);
- end;
- if (theYear <= 0) then
- begin
- exit(DrawMonth);
- end;
- if (thePort = nil) then
- begin
- exit(DrawMonth);
- end;
-
- GetPort(oldPort);
- SetPort(thePort);
-
- GetFontInfo(fInfo);
-
- {• Initalize starting position.}
- pos := start;
-
- {• Get size of row.}
- rowSize := fInfo.ascent + fInfo.descent + fInfo.leading;
-
- horizCenter := (kRectWidth + kHSpacing) div 2; {• Calculate center position.}
-
- {• Get half size of month name.}
- mWidth := StringWidth(gpMonthsOfYear[theMonth]) div 2;
-
- {• draw month name - centered •//}
-
- MoveTo(pos.h + (horizCenter - mWidth), pos.v + (rowSize * currRow));
- DrawString(gpMonthsOfYear[theMonth]);
-
- {• move down one row •//}
- currRow := currRow + 1;
-
- {Fine tuning (should have been automatic, really)}
- pos.h := pos.h + 2;
- pos.v := pos.v + 3; {• plus a little more •//}
-
- {• draw the days header •//}
-
- MoveTo(pos.h, pos.v + (rowSize * currRow));
- DrawString(gDaysString);
-
- currRow := currRow + 1; {• move down one row •//}
-
- {• Draw the line separator •//}
- MoveTo(pos.h - 2, pos.v + (rowSize * currRow) - (rowSize div 2));
- PenSize((thePort^.txSize div 9), (thePort^.txSize div 9));
- Line((kRectWidth) - 1, 0);
-
- {• move down one row •//}
- currRow := currRow + 1;
-
- {• Draw the first day of the month}
- tmp := DayOfMonth(1, theMonth, theYear);
- DrawDay(1, tmp);
-
- {• Get the number of days in month the particular month.}
- {• Assign that value to numDays.}
- numDays := DaysInMonth(theMonth, theYear);
-
- {• Cycle through day 2 to the last day, drawing each day.}
- for dayIndex := 2 to numDays do
- begin
- {• get day of week Sunday to Saturday.}
- ddayOfMonth := DayOfMonth(dayIndex, theMonth, theYear);
-
- {• if Sunday, move to next line •//}
-
- if (ddayOfMonth = SUNDAY) then
- begin
- currRow := currRow + 1;
- end;
- {• draw the day at the right location •//}
- DrawDay(dayIndex, ddayOfMonth);
-
- end; {• for •//}
-
- SetPort(oldPort);
- end; {• DrawMonth() •//}
-
- {The following must be globals since the main routine needs them}
- var
- janRect, febRect, marRect, aprRect, mayRect, junRect, julRect, augRect, sepRect, octRect, novRect, decRect: Rect;
-
- procedure Rectify;
- function MakeOffsetRect (r: Rect; hOffs, vOffs: integer): Rect;
- begin
- OffsetRect(r, hOffs, vOffs);
- MakeOffsetRect := r;
- end; {MakeOffsetRect}
- begin
- SetRect(JanRect, 0, 0, kRectWidth, kRectHeight); {• 104 by 124.}
- OffsetRect(janRect, kFirstHOffset, kFirstVOffset);
- febRect := MakeOffsetRect(janRect, kRectWidth + kHSpacing, 0);
- marRect := MakeOffsetRect(febRect, kRectWidth + kHSpacing, 0);
- aprRect := MakeOffsetRect(marRect, kRectWidth + kHSpacing, 0);
-
- mayRect := MakeOffsetRect(janRect, 0, kRectHeight + kVSpacing);
-
- junRect := MakeOffsetRect(mayRect, kRectWidth + kHSpacing, 0);
- julRect := MakeOffsetRect(junRect, kRectWidth + kHSpacing, 0);
- augRect := MakeOffsetRect(julRect, kRectWidth + kHSpacing, 0);
-
- sepRect := MakeOffsetRect(mayRect, 0, kRectHeight + kVSpacing);
-
- octRect := MakeOffsetRect(sepRect, kRectWidth + kHSpacing, 0);
- novRect := MakeOffsetRect(octRect, kRectWidth + kHSpacing, 0);
- decRect := MakeOffsetRect(novRect, kRectWidth + kHSpacing, 0);
-
- FrameRect(janRect);
- FrameRect(febRect);
- FrameRect(marRect);
- FrameRect(aprRect);
- FrameRect(mayRect);
- FrameRect(junRect);
- FrameRect(julRect);
- FrameRect(augRect);
- FrameRect(sepRect);
- FrameRect(octRect);
- FrameRect(novRect);
- FrameRect(decRect);
- end; {Rectify}
-
- procedure InitArrays (year: integer);
- var
- i: integer;
- begin
- for i := 1 to 12 do
- begin
- gMonthDays[i] := DaysInMonth(i, year);
- end;
-
- for i := 1 to 7 do
- GetIndString(gpDaysOfWeek[i], kWeekdayStrings, i);
- for i := 1 to 12 do
- GetIndString(gpMonthsOfYear[i], kMonthStrings, i);
-
- gDaysString := GetString(kDaysStringId)^^;
- end;
-
- function AskYear: integer;
- var
- dialog: DialogPtr;
- oldPort: GrafPtr;
- itemHit: integer;
- str: str255;
-
- kind: integer;
- item: ControlHandle;
- box: Rect;
- year: Longint;
- begin
- GetPort(oldPort);
- dialog := GetNewDialog(kYearDlogID, nil, WindowPtr(-1));
- SetPort(dialog);
-
- SelIText(dialog, 3, 0, 32767);
- itemHit := -1;
- while itemHit <> 1 do
- ModalDialog(nil, itemHit);
- GetDItem(dialog, 3, kind, Handle(item), box);
- GetIText(handle(item), str);
- StringToNum(str, year);
- AskYear := year;
- DisposeDialog(dialog);
- SetPort(oldPort);
- end;
-
- {• --------------------------------------------------------------- •//}
- {• main() •//}
- {• Main test procedure. •//}
- {• --------------------------------------------------------------- •//}
-
- {procedure main;}
- var
- monthWindow: WindowPtr;
- f: FontInfo;
- pic: PicHandle;
- ignore: integer;
- theYear: integer;
- title: Str255;
- begin
- {Toolbox init is automatic.}
-
- theYear := AskYear;
-
- InitArrays(theYear); {Year should be variable!}
-
- monthWindow := GetNewWindow(kCalendarWindowId, nil, WindowPtr(-1));
-
- if monthWindow <> nil then
- begin
- CenterWindow(monthWindow);
- SetPort(monthWindow);
- ShowWindow(monthWindow);
-
- NumToString(theYear, title);
-
- {Draw into a picture.}
- pic := OpenPicture(monthWindow^.portRect);
-
- SetDefaultText(monaco, [bold], 12); {• Monaco fits all in}
- GetFontInfo(f); {• a 512x384 screen.}
- MoveTo(monthWindow^.portRect.right div 2 - StringWidth(title) div 2, f.ascent + f.descent + f.leading);
- DrawString(title);
-
- SetDefaultText(monaco, [], 9); {• Monaco fits all in}
- GetFontInfo(f); {• a 512x384 screen.}
-
- Rectify;
- DrawMonth(monthWindow, janRect.topLeft, JANUARY, theYear);
- DrawMonth(monthWindow, febRect.topLeft, FEBRUARY, theYear);
- DrawMonth(monthWindow, marRect.topLeft, MARCH, theYear);
- DrawMonth(monthWindow, aprRect.topLeft, APRIL, theYear);
- DrawMonth(monthWindow, mayRect.topLeft, MAY, theYear);
- DrawMonth(monthWindow, junRect.topLeft, JUNE, theYear);
- DrawMonth(monthWindow, julRect.topLeft, JULY, theYear);
- DrawMonth(monthWindow, augRect.topLeft, AUGUST, theYear);
- DrawMonth(monthWindow, sepRect.topLeft, SEPTEMBER, theYear);
- DrawMonth(monthWindow, octRect.topLeft, OCTOBER, theYear);
- DrawMonth(monthWindow, novRect.topLeft, NOVEMBER, theYear);
- DrawMonth(monthWindow, decRect.topLeft, DECEMBER, theYear);
- ClosePicture;
-
- DrawPicture(pic, pic^^.picFrame);
-
- ignore := LoadScrap;
- ignore := ZeroScrap;
- HLock(Handle(pic));
- ignore := PutScrap(GetHandleSize(Handle(pic)), 'PICT', pointer(Handle(pic)^));
- if SystemEdit(3) then
- ; { so multifinder will take the scrap }
- HUnLock(Handle(pic));
- ignore := UnLoadScrap;
-
- FlushEvents(everyEvent, 0);
-
- while not Button do {• Do it until THE Button is hit.}
- ;
-
- FlushEvents(everyEvent, 0); {• Then act like nothing happened.}
- DisposeWindow(monthWindow); {• And then dump everything}
- end;
- end. {• "Get back home, Loretta!"}
-
- {•*************************************************************************************************•//}